package com.zwr.sheller.shop.service.impl;

import com.zwr.sheller.core.common.WQQBusinessException;
import com.zwr.sheller.core.common.WQQExceptionCode;
import com.zwr.sheller.core.pojo.Store;
import com.zwr.sheller.core.util.GetThreadLocal;
import com.zwr.sheller.core.util.OSSUtil;
import com.zwr.sheller.core.util.ThreadLocalUtil;
import com.zwr.sheller.shop.mapper.StoreMapper;
import com.zwr.sheller.shop.service.StoreBackgroundAndServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: 18032
 * @Date: 15:55 2022/1/10
 */
@Service
public class StoreBackgroundAndServiceServiceImpl implements StoreBackgroundAndServiceService {

    @Autowired(required = false)
    StoreMapper storeMapper;

    @Override
    public Integer addBackground(Store store) throws WQQBusinessException {
        Map<String,Object> map = new HashMap<>(1);
        map.put("storeId",1L);
        ThreadLocalUtil.setObject(map);

        Long storeId = GetThreadLocal.getStoreId();
        store.setStoreId(storeId);

        //复制图片到正式文件
        if (!StringUtils.isEmpty(store.getStoreBackground())) {
            String fileName =store.getStoreBackground().substring(37);
            OSSUtil.copyFile("temporary/" + fileName, "signPicture" + fileName);
            OSSUtil.delete("temporary/" + fileName);
            store.setStoreBackground("http://47.108.60.93/ossImg/storePicture" + fileName);
        }
        int i = storeMapper.updateByPrimaryKeySelective(store);
        if (i == 0){
            throw new WQQBusinessException(WQQExceptionCode.UPDATE_FALSE);
        }
        return i;
    }

    @Override
    public Integer update12hour(Boolean update) throws WQQBusinessException {
        Map<String,Object> map = new HashMap<>(1);
        map.put("storeId",1L);
        ThreadLocalUtil.setObject(map);

        Long storeId = GetThreadLocal.getStoreId();
        Store store = new Store();
        store.setStoreId(storeId);
        store.setStore12hour(update);
        int i = storeMapper.updateByPrimaryKeySelective(store);
        if (i == 0){
            throw new WQQBusinessException(WQQExceptionCode.UPDATE_FALSE);
        }
        return i;
    }

    @Override
    public Integer quality(Boolean update) throws WQQBusinessException {
        Map<String,Object> map = new HashMap<>(1);
        map.put("storeId",1L);
        ThreadLocalUtil.setObject(map);

        Long storeId = GetThreadLocal.getStoreId();
        Store store = new Store();
        store.setStoreId(storeId);
        store.setStoreQuality(update);
        int i = storeMapper.updateByPrimaryKeySelective(store);
        if (i == 0){
            throw new WQQBusinessException(WQQExceptionCode.UPDATE_FALSE);
        }

        return i;
    }

    @Override
    public Integer giveNewGood(Boolean update) throws WQQBusinessException {
        Map<String,Object> map = new HashMap<>(1);
        map.put("storeId",1L);
        ThreadLocalUtil.setObject(map);

        Long storeId = GetThreadLocal.getStoreId();
        Store store = new Store();
        store.setStoreId(storeId);
        store.setStoreAddNew(update);
        int i = storeMapper.updateByPrimaryKeySelective(store);
        if (i == 0){
            throw new WQQBusinessException(WQQExceptionCode.UPDATE_FALSE);
        }
        return i;
    }
}
